package store.lunangangster.nodeflow.parser.el.operator;

import static store.lunangangster.nodeflow.enums.FlowType.FLOW_SWITCH;

import cn.hutool.core.collection.CollUtil;
import com.ql.util.express.Operator;
import store.lunangangster.nodeflow.builder.FlowBuilder;
import store.lunangangster.nodeflow.core.component.SwitchComponent;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.parser.el.OperatorHelper;
import store.lunangangster.nodeflow.rt.element.Node;
import store.lunangangster.nodeflow.util.Converter;

/**
 * Switch表达式所关联的操作符
 *
 * @author cwk
 * @version 1.0
 */
public class SwitchOperator extends Operator {

  @Override
  public Object executeInner(Object[] list) throws RuleSourceParseException {
    OperatorHelper.checkArguments(list);

    Node switchNode = Converter.convert(list[0], Node.class);
    if (!switchNode.isComponent(SwitchComponent.class)) {
      throw new RuleSourceParseException("Incompatible executable for SwitchFlow: should be like SWITCH(switchNode).TO(a, b, c...)");
    }
    return FlowBuilder.buildELFLow(FLOW_SWITCH, CollUtil.toList(switchNode));
  }
}
